package com.chuji.crmcj.act;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.InsureCountInfo;
import com.chuji.crmcj.utils.ConstantValue;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.view.InsureBar;
import com.chuji.crmcj.view.SimpleBottomDialog;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by w.pitt on 2016/2/3.
 */
public class InsureCalAct extends BaseActivity implements View.OnClickListener {
    private TextView mTv_count_money;
    private EditText mEt_input_money;
    private Button mBtn_relevance_client;
    private String mItemMoney;
    private String costHis;
    private Integer oldHistory;
    private View ll_left;
    private String third;
    private String carLoss;
    private String carLossBef;
    private TextView mTvThirdCost;
    private TextView mTvJqxCost;
    private InsureBar jiaoqiangxian;
    private InsureBar mThirdxian;
    private InsureBar mDaoqiangxian;
    private InsureBar mZiranxian;
    private InsureBar mWuguoxian;
    private InsureBar mHuahenxian;
    private InsureBar mBolixian;
    private InsureBar mSunshixian;
    private InsureBar mMianpeixian;
    private InsureBar mZerenxian;
    private TextView mTvHhCost;
    private TextView mTvBoliCost;
    private List<Boolean> mValue = new ArrayList<Boolean>();
    private List<CheckBox> mKey = new ArrayList<CheckBox>();
    private List<TextView> mTextViews = new ArrayList<TextView>();
    private TextView mTvDq;
    private TextView mTvSs;
    private TextView mTvZiran;
    private TextView mTvMp;
    private TextView mTvWg;
    private TextView mTvZeren;
    private double thirdMoney;
    private double carLossMoney;
    private String luoche;
    private CheckBox mCbJqx;
    private CheckBox mCbHh;
    private CheckBox mCbThird;
    private CheckBox mCbBoli;
    private CheckBox mCbQd;
    private CheckBox mCbSs;
    private CheckBox mCbZiran;
    private CheckBox mCbMp;
    private CheckBox mCbWuguo;
    private CheckBox mCbZeren;
    private int luocheMoney;
    private int oldMoney;
    private long addMoney;
    private int originCarCost;
    private int beforMoney;
    private String itemCost;
    private SimpleBottomDialog dialog;
    private Double boliRate;

    @Override
    protected void initView() {
        setContentView(R.layout.act_insure_cal);
        mTv_count_money = (TextView) findViewById(R.id.tv_count_money);
        mEt_input_money = (EditText) findViewById(R.id.et_input_money);
        jiaoqiangxian = (InsureBar) findViewById(R.id.jiaoqiangxian);
        mThirdxian = (InsureBar) findViewById(R.id.thirdxian);
        mDaoqiangxian = (InsureBar) findViewById(R.id.daoqiangxian);
        mZiranxian = (InsureBar) findViewById(R.id.ziranxian);
        mWuguoxian = (InsureBar) findViewById(R.id.wuguoxian);
        mHuahenxian = (InsureBar) findViewById(R.id.huahenxian);
        mBolixian = (InsureBar) findViewById(R.id.bolixian);
        mSunshixian = (InsureBar) findViewById(R.id.sunshixian);
        mMianpeixian = (InsureBar) findViewById(R.id.mianpeixian);
        mZerenxian = (InsureBar) findViewById(R.id.zerenxian);
        mBtn_relevance_client = (Button) findViewById(R.id.btn_relevance_client);
        ll_left = findViewById(R.id.ll_left);
        mTvJqxCost = jiaoqiangxian.getInsCost();

        LinearLayout jqxRight = jiaoqiangxian.getLlRightIcon();
        jqxRight.setVisibility(View.VISIBLE);

        mTvThirdCost = mThirdxian.getInsCost();
        LinearLayout thirdRight = mThirdxian.getLlRightIcon();
        thirdRight.setVisibility(View.VISIBLE);

        mTvHhCost = mHuahenxian.getInsCost();
        LinearLayout hhRight = mHuahenxian.getLlRightIcon();
        hhRight.setVisibility(View.VISIBLE);


        mTvBoliCost = mBolixian.getInsCost();
        LinearLayout boliRight = mBolixian.getLlRightIcon();
        boliRight.setVisibility(View.VISIBLE);

        mTvDq = mDaoqiangxian.getInsCost();
        mTvSs = mSunshixian.getInsCost();
        mTvZiran = mZiranxian.getInsCost();
        mTvMp = mMianpeixian.getInsCost();
        mTvWg = mWuguoxian.getInsCost();
        mTvZeren = mZerenxian.getInsCost();

        mCbJqx = jiaoqiangxian.getCheck();
        mCbHh = mHuahenxian.getCheck();
        mCbThird = mThirdxian.getCheck();
        mCbBoli = mBolixian.getCheck();
        mCbQd = mDaoqiangxian.getCheck();
        mCbSs = mSunshixian.getCheck();
        mCbZiran = mZiranxian.getCheck();
        mCbMp = mMianpeixian.getCheck();
        mCbWuguo = mWuguoxian.getCheck();
        mCbZeren = mZerenxian.getCheck();

        mKey.add(mCbJqx);
        mKey.add(mCbHh);
        mKey.add(mCbThird);
        mKey.add(mCbBoli);
        mKey.add(mCbQd);
        mKey.add(mCbSs);
        mKey.add(mCbZiran);
        mKey.add(mCbMp);
        mKey.add(mCbWuguo);
        mKey.add(mCbZeren);

        mTextViews.add(mTvJqxCost);
        mTextViews.add(mTvHhCost);
        mTextViews.add(mTvThirdCost);
        mTextViews.add(mTvBoliCost);
        mTextViews.add(mTvDq);
        mTextViews.add(mTvSs);
        mTextViews.add(mTvZiran);
        mTextViews.add(mTvMp);
        mTextViews.add(mTvWg);
        mTextViews.add(mTvZeren);

        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
        mValue.add(false);
//        searchCheckList();
    }

    private void searchCheckList() {
        //循环遍历Value  有几个选中的
        for (int i = 0; i < mKey.size(); i++) {
            mValue.set(i, mKey.get(i).isChecked());
        }
//        luoche = mEt_input_money.getText().toString().trim();

//        if (luoche.equals("") || luoche.equals("0")) {
//            luocheMoney = 0;
//        } else {
//            luocheMoney = Integer.parseInt(luoche);
//        }
        int totalMoney = 0;
        for (int i = 0; i < mValue.size(); i++) {
            if (mValue.get(i)) {
                totalMoney = totalMoney + (Integer.parseInt(mTextViews.get(i).getText().toString()));
            }
        }
        mTv_count_money.setText(String.valueOf(totalMoney));
    }

    @Override
    protected void initData() {
        mBtnBarRight.setVisibility(View.VISIBLE);
        mBtnBarRight.setText("历史用户");
        mTvBarTitle.setText("保险计算器");
    }

    private int checkItem = -1;
    private int lable = -1;

    @Override
    protected void initListener() {
        mEt_input_money.addTextChangedListener(mTextWatcher);
        mBtnBarRight.setOnClickListener(this);
        ll_left.setOnClickListener(this);
        mBtn_relevance_client.setOnClickListener(this);

        jiaoqiangxian.setOnClickListener(this);
        mThirdxian.setOnClickListener(this);
        mDaoqiangxian.setOnClickListener(this);
        mZiranxian.setOnClickListener(this);
        mWuguoxian.setOnClickListener(this);
        mHuahenxian.setOnClickListener(this);
        mBolixian.setOnClickListener(this);
        mSunshixian.setOnClickListener(this);
        mMianpeixian.setOnClickListener(this);
        mZerenxian.setOnClickListener(this);


        mCbJqx = jiaoqiangxian.getCheck();
        mCbHh = mHuahenxian.getCheck();
        mCbThird = mThirdxian.getCheck();
        mCbBoli = mBolixian.getCheck();
        mCbQd = mDaoqiangxian.getCheck();
        mCbSs = mSunshixian.getCheck();
        mCbZiran = mZiranxian.getCheck();
        mCbMp = mMianpeixian.getCheck();
        mCbWuguo = mWuguoxian.getCheck();
        mCbZeren = mZerenxian.getCheck();

        mCbJqx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbHh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbThird.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbQd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbSs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbZiran.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbMp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbWuguo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
        mCbZeren.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                searchCheckList();
            }
        });
    }

    private Integer beforHisCos;
    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

//        if (originCarCost.equals("") || oldCost.equals("0")) {
//            oldMoney = 0;
//        } else {
//            oldMoney = Integer.parseInt(oldCost);
//        }

            if (s.toString().equals("") || s.toString().equals("0")) {
                addMoney = 0;
            } else {
                addMoney = Long.parseLong(s.toString());
            }
//           mTv_count_money.setText(String.valueOf(oldMoney + addMoney));
            //计算 其余  保险金额
            setCountItemCost();
            searchCheckList();
        }

        @Override
        public void afterTextChanged(Editable s) {
            //裸车价格  定死
            if (s.toString().equals("") || s.toString().equals("0")) {
                originCarCost = 0;
            } else {
                originCarCost = Integer.parseInt(s.toString());
            }
        }
    };

    private void setCountItemCost() {
        String thirdCost = mTvThirdCost.getText().toString().trim();
        //全车盗抢险
        setInsItemCost(100.00, 0.00453, mTvDq);

        //车辆损失险
        setInsItemCost(457.00, 0.01088, mTvSs);

        //自燃损失险
        setInsItemCost(0.0, 0.0015, mTvZiran);

        //不计免赔险
        String carLossBef = mTvSs.getText().toString().trim();
        if (thirdCost.equals("") || thirdCost.equals("0")) {
            thirdMoney = 0;
        } else {
            thirdMoney = Double.parseDouble(thirdCost);
        }
        if (carLossBef.equals("") || carLossBef.equals("0")) {
            carLossMoney = 0;
        } else {
            carLossMoney = Double.parseDouble(carLossBef);
        }
        mTvMp.setText(String.valueOf(((int) ((thirdMoney + carLossMoney) * 0.2))));

        // 无过责任险
        if (thirdCost.equals("") || thirdCost.equals("0")) {
            thirdMoney = 0;
        } else {
            thirdMoney = Double.parseDouble(thirdCost);
        }
        mTvWg.setText(String.valueOf((int) (thirdMoney * 0.2)));

        //车上人员险
        mTvZeren.setText("50");
    }

    private void setInsItemCost(double base, double rate, TextView mCost) {
        luoche = mEt_input_money.getText().toString().trim();
        if (luoche.equals("0") || luoche.equals("")) {
            mCost.setText(String.valueOf(0));
        } else {
            double cost = Double.parseDouble(luoche);
            if (cost == 0 || cost < 0) {
                cost = 0;
            }
            int cost1 = (int) (base + cost * rate);
//            if(mCost==mTvJqxCost){
//
//            }else if(){
//
//            }
//            else if(){
//
//            }else if(){
//
//            }else if(){
//
//            }else if(){
//
//            }else if(){
//
//            }else if(){
//
//            }
            mCost.setText(String.valueOf(cost1));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_left:
                finish();
                break;
            case R.id.btn_right:
                UIUtils.startActivity(new Intent(this, InsureHistoryUseAct.class));
                break;
            case R.id.btn_relevance_client:
                //拼接参数
                saveInsureData();

                Intent intent = new Intent(InsureCalAct.this, SearchActivity.class);
                intent.putExtra("Lable", ConstantValue.SEARCHSELF);
                UIUtils.startActivity(intent);
                break;
            // 有弹出框  保险item
            case R.id.jiaoqiangxian:
                showTestDialog(InsureCalAct.this, 0, mTvJqxCost);
                break;
            case R.id.huahenxian:
                showTestDialog(InsureCalAct.this, 1, mTvHhCost);
                break;
            case R.id.thirdxian:
                showTestDialog(InsureCalAct.this, 2, mTvThirdCost);
                break;
            case R.id.bolixian:
                showTestDialog(InsureCalAct.this, 3, mTvBoliCost);
                break;
        }
    }

    private void saveInsureData() {
        //循环遍历Value  有几个选中的
        for (int i = 0; i < mKey.size(); i++) {
            mValue.set(i, mKey.get(i).isChecked());
        }

        InsureCountInfo insureCountInfo = new InsureCountInfo();
        StringBuffer buffer = new StringBuffer();
//        new InsureCountInfo.jiaoqiangxian.;

        buffer.append("{");
        for (int i = 0; i < mValue.size(); i++) {
            itemCost = mTextViews.get(i).getText().toString();
            int parseInt = Integer.parseInt(itemCost);
            if (mValue.get(i)) {
                switch (i) {
                    case 0:
                        //交强险：
                        if (parseInt == 950) {
                            buffer.append("\"交强险\":{\"家用六座以下\":true,\"家用六座以上\":false,\"Amount\":950},");
                        } else if (parseInt == 1100) {
                            buffer.append("\"交强险\":{\"家用六座以下\":false,\"家用六座以上\":true,\"Amount\":1100},");
                        } else {
                            buffer.append("\"交强险\":{\"家用六座以下\":false,\"家用六座以上\":false,\"Amount\":0},");
                        }
                        break;
                    case 1:
                        //划痕
                        if (parseInt == 400) {
                            buffer.append("\"车身划痕险\":{\"2千\":true,\"5千\":false,\"1万\":false,\"2万\":true,\"Amount\":400},");
                        } else if (parseInt == 570) {
                            buffer.append("\"车身划痕险\":{\"2千\":false,\"5千\":true,\"1万\":false,\"2万\":true,\"Amount\":570},");
                        } else if (parseInt == 760) {
                            buffer.append("\"车身划痕险\":{\"2千\":false,\"5千\":false,\"1万\":true,\"2万\":true,\"Amount\":760},");
                        } else if (parseInt == 1140) {
                            buffer.append("\"车身划痕险\":{\"2千\":false,\"5千\":false,\"1万\":false,\"2万\":true,\"Amount\":1140},");
                        } else {
                            buffer.append("\"车身划痕险\":{\"2千\":false,\"5千\":false,\"1万\":false,\"2万\":false,\"Amount\":0},");
                        }
                        break;
                    case 2:
                        if (parseInt == 516) {
                            buffer.append("\"车身划痕险\":{\"5万\":true,\"10万\":false,\"20万\":false,\"50万\":false,\"100万\":false,\"Amount\":516},");
                        } else if (parseInt == 746) {
                            buffer.append("\"车身划痕险\":{\"5万\":false,\"10万\":true,\"20万\":false,\"50万\":false,\"100万\":false,\"Amount\":746},");
                        } else if (parseInt == 925) {
                            buffer.append("\"车身划痕险\":{\"5万\":false,\"10万\":false,\"20万\":true,\"50万\":false,\"100万\":false,\"Amount\":925},");
                        } else if (parseInt == 1252) {
                            buffer.append("\"车身划痕险\":{\"5万\":false,\"10万\":false,\"20万\":false,\"50万\":true,\"100万\":false,\"Amount\":1252},");
                        } else if (parseInt == 1630) {
                            buffer.append("\"车身划痕险\":{\"5万\":false,\"10万\":false,\"20万\":false,\"50万\":false,\"100万\":true,\"Amount\":1630},");
                        } else {
                            buffer.append("\"车身划痕险\":{\"5万\":false,\"10万\":false,\"20万\":false,\"50万\":false,\"100万\":false,\"Amount\":0},");

                        }
                        break;
                    case 3:
                        if (boliRate != null) {
                            if (boliRate == 0.0015) {
                                buffer.append("\"玻璃单独破碎险\":{\"国产\":true,\"进口\":false,\"Amount\":" + itemCost + "},");
                            } else if (boliRate == 0.0025) {
                                buffer.append("\"玻璃单独破碎险\":{\"国产\":false,\"进口\":true,\"Amount\":" + itemCost + "},");
                            }
                        } else {
                            buffer.append("\"玻璃单独破碎险\":{\"国产\":false,\"进口\":false,\"Amount\":" + itemCost + "},");
                        }
                        break;
                    case 4:
                        buffer.append("\"全车盗抢险\":{\"Amount\":" + Integer.parseInt(itemCost) + "},");
                        break;
                    case 5:
                        buffer.append("\"车身损失险\":{\"Amount\":" + Integer.parseInt(itemCost) + "},");
                        break;
                    case 6:
                        buffer.append("\"自燃损失险\":{\"Amount\":" + Integer.parseInt(itemCost) + "},");
                        break;
                    case 7:
                        buffer.append("\"不计免赔险\":{\"Amount\":" + Integer.parseInt(itemCost) + "},");
                        break;
                    case 8:
                        buffer.append("\"无过责任险\":{\"Amount\":" + Integer.parseInt(itemCost) + "},");
                        break;
                    case 9:
                        buffer.append("\"车上人员责任险\":{\"Amount\":" + Integer.parseInt(itemCost) + "}}");
                        break;
                }
            } else {
                switch (i) {
                    case 0:
                        //交强险：
                        buffer.append("\"交强险\":{\"家用六座以下\":false,\"家用六座以上\":false,\"Amount\":0},");
                        break;
                    case 1:
                        //划痕
                        buffer.append("\"车身划痕险\":{\"2千\":false,\"5千\":false,\"1万\":false,\"2万\":false,\"Amount\":0},");
                        break;
                    case 2:
                        buffer.append("\"车身划痕险\":{\"5万\":false,\"10万\":false,\"20万\":false,\"50万\":false,\"100万\":false,\"Amount\":0},");
                        break;
                    case 3:
                        buffer.append("\"玻璃单独破碎险\":{\"国产\":false,\"进口\":false,\"Amount\":0},");
                        break;
                    case 4:
                        buffer.append("\"全车盗抢险\":{\"Amount\":0},");
                        break;
                    case 5:
                        buffer.append("\"车身损失险\":{\"Amount\":0},");
                        break;
                    case 6:
                        buffer.append("\"自燃损失险\":{\"Amount\":0},");
                        break;
                    case 7:
                        buffer.append("\"不计免赔险\":{\"Amount\":0},");
                        break;
                    case 8:
                        buffer.append("\"无过责任险\":{\"Amount\":0},");
                        break;
                    case 9:
                        buffer.append("\"车上人员责任险\":{\"Amount\":0}}");
                        break;
                }

            }
        }
        System.out.println(buffer.toString());
    }

    Map<Integer, Boolean> isCheckMap = new LinkedHashMap<Integer, Boolean>();

    private double selectValue = -1.0;
    private int selectLable = -1;
//    class GvInsureAda extends BaseAdapter {
//        private List<String> mInsures = new ArrayList<String>(Arrays.asList("交强险", "车身划痕险", "第三者险", "玻璃单独破碎险", "全车盗抢险", "车身损失险", "自燃损失险", "不计免赔险", "无过责任险", "车上人员责任险"));
//        private List<Boolean> mCheck = new ArrayList<Boolean>();
//        private GridView mGvExtraInsure;
//        private Context mContext;
//
//        public GvInsureAda(InsureCalAct insureCalAct, GridView mGvExtraInsure) {
//            this.mGvExtraInsure = mGvExtraInsure;
//            this.mContext = insureCalAct;
//        }
//
//        @Override
//        public int getCount() {
//            return mInsures.size();
//        }
//
//        @Override
//        public Object getItem(int position) {
//            return mInsures.get(position);
//        }
//
//        @Override
//        public long getItemId(int position) {
//            return position;
//        }
//
//        @Override
//        public View getView(final int position, View convertView, ViewGroup parent) {
//            convertView = View.inflate(UIUtils.getContext(), R.layout.layout_extra_insure, null);
//            return convertView;
//        }
//    }

    protected void showTestDialog(Context mContext, final int lable, final TextView costView) {
        dialog = new SimpleBottomDialog(lable, mContext);
        dialog.setOnOKClickListener(new SimpleBottomDialog.OnOKClickListener() {
            @Override
            public void onOKClick(View v, int position, Map<String, Double> map, int lable1) {
                values = new ArrayList<Double>();
                for (Map.Entry<String, Double> entry : map.entrySet()) {
                    values.add(entry.getValue());
                }
                selectLable = lable1;
                selectValue = values.get(position);
                // 判断是否选中
                if (mValue.get(lable)) {
                    // 先拿到 总金额：
                    // 拿到未改变前的金额
                    // 总金额减去 未改变前的金额
                    // 再 searchCheckList();
                    String totleCost = mTv_count_money.getText().toString().trim();
                    int totleMoney = Integer.parseInt(totleCost);
                    if (totleCost.equals("") || totleCost.equals("0")) {
                        totleMoney = 0;
                    } else {
                        totleMoney = Integer.parseInt(totleCost);
                    }
                    String beforeCost = costView.getText().toString().trim();
                    if (beforeCost.equals("") || beforeCost.equals("0")) {
                        beforMoney = 0;
                    } else {
                        beforMoney = Integer.parseInt(beforeCost);
                    }
                    int afterTotle = totleMoney - beforMoney;
                    mTv_count_money.setText(String.valueOf(afterTotle));
                }
                //没选中的话
                //直接赋值
                //完了遍历
                if (lable == 3) {
                    //玻璃险
                    boliRate = selectValue;
                    setInsItemCost(0.0, selectValue, mTvBoliCost);
                } else {
                    costView.setText(String.valueOf((int) selectValue));
                }
                setCountItemCost();
                searchCheckList();
            }
        });
        dialog.show();
    }

    private List<Double> values;
    private CheckBox iv_check;
    private TextView tv_extra_name;
    private TextView tv_extra_cost;
    private LinearLayout ll_click;

}
